package com.sniper.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by suzhen on 2017/6/16.
 */
public class HadoopHolder {

    public String url = "hdfs://master:9000";
    String uploadFileLocal = "/Users/suzhen/Desktop/WechatIMG112.jpeg";
    String uploadFileRemote = "/WechatIMG112.jpeg";
    String uploadDirLocal = "/Users/suzhen/Desktop";
    String uploadDirRemote = "/sniper";

    FileSystem fileSystem;


    @Before
    public void init() throws URISyntaxException, IOException {

        Configuration configuration = new Configuration();
        fileSystem = FileSystem.get(new URI(url), configuration);

    }

    @Test
    public void setFileSystem() {
        System.out.println(fileSystem);
    }

    @Test
    @Ignore
    public void download() throws IOException {

        //文件下载
        FSDataInputStream in = fileSystem.open(new Path(uploadFileRemote));
        FileOutputStream out = new FileOutputStream(uploadDirLocal + "/hadoop.jpeg");
        IOUtils.copyBytes(in, out, 4096, true);
        //上传
    }

    @Test
    public void upload() throws IOException {
        //上传
        FileInputStream in = new FileInputStream(uploadFileLocal);
        FSDataOutputStream out = fileSystem.create(new Path(uploadDirRemote + uploadFileRemote));
        IOUtils.copyBytes(in, out, 4096, true);
    }

    @Test
    public void delete() throws IOException {
        //单个文件或空文件选择false,删除多层数据用true
        boolean flag = fileSystem.delete(new Path(uploadFileRemote), true);
        System.out.println(flag);
    }

    @Test
    public void mkdir() throws IOException {
        boolean flag = fileSystem.mkdirs(new Path(uploadDirRemote));
        System.out.println(flag);
    }
}
